package com.gemptc.wode.service;

import java.io.IOException;
import java.util.Iterator;
import java.util.List;

import org.apache.commons.fileupload.DiskFileUpload;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;

import com.gemptc.daoimp.SelectImp;
import com.gemptc.wode.daoImp.Select_WouserImp;
import com.jspsmart.upload.SmartUpload;
import com.jspsmart.upload.SmartUploadException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/imageservlet")
public class ImageServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private String file;

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		//System.out.println("小超哈哈哈");

		//String username = request.getParameter("username");
		
		String username = null;
		long fl = (new java.util.Date()).getTime();
		String s = String.valueOf(fl + ".jpg");

		DiskFileUpload fu = new DiskFileUpload();
		List fileItems = null;
		try {
			fileItems = fu.parseRequest(request);
			// 依次处理每个上传的文件
			Iterator iter = fileItems.iterator();
			while (iter.hasNext()) {
				FileItem item = (FileItem) iter.next();
				String  filedname=item.getFieldName();
				//System.out.println("jiaojiao" + item.getString());
				if (item.isFormField()) {
					if (filedname.equals("username")) {
						username=item.getString();
						System.out.println("hhhhhhhh" + username);
					}
					request.setAttribute(item.getFieldName(), item.getString());
					System.out.println("item.getFieldName()-->"
							+ item.getFieldName() + "=" + item.getString());
				} else {
					item.write(new java.io.File(
							/* this.getServletContext().getRealPath("/") +"img\\"*/
							"D:\\workspace\\Jinnang7\\WebContent\\img\\" + s));
				}   
				//"D:\\workspace\\Jinnang7\\WebContent\\img\\"
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	//System.out.println("已上传完成114");

		// --------------------------------
		//System.out.println("接收到头像");
		System.out.println("小超哈哈哈" + username);
		SelectImp selectImp = new SelectImp();
		int user_id = selectImp.SelectUserId(username);
		Select_WouserImp select_WouserImp2 = new Select_WouserImp();
		select_WouserImp2.update1(s, user_id);
		System.out.println("插入到数据库");

	}
}
